// import Cookies from 'js-cookie';

const app = {
  state: {
    sidebar: {
      opened: !+localStorage.getItem('sidebarStatus')
    },
    commonLoading:false,
    theme: 'default',
    visitedViews: [],
    showRouteView:true,
    iframeActive:'',
    iframeArray:[],
    iframeArrayIndexShow:'',
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        localStorage.setItem('sidebarStatus', 1);
      } else {
        localStorage.setItem('sidebarStatus', 0);
      }
      state.sidebar.opened = !state.sidebar.opened;
    },
    commonLoading(state,data){
      state.commonLoading = data
    },
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push({ name: view.name, path:view.path,id:view.id,viewType:'tab'})
    },
    DEL_VISITED_VIEWS: (state, view) => {
      let index
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view) {
          index = i
          break
        }
      }
      state.visitedViews.splice(index, 1)
    },
    goIframe: (state, data) => {
      if(data){
        state.iframeActive=data.path;
        state.iframeArrayIndexShow=data.path;
        state.showRouteView = false;
      }else{
        state.iframeActive='';
        state.showRouteView =true;
      }
    },
    ADD_IFRAME:(state, data) => {
      if (state.iframeArray.some(v => v.path === data.path)) return
      state.iframeArray.push(data)
    },
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    toggleIframe({commit},data){
      if(data){
        commit('ADD_VISITED_VIEWS', data)
        commit('goIframe', data);
      }else{
        commit('goIframe');
      }
    },
    addIframe({ commit }, data) {
      commit('ADD_IFRAME', data)
    },
  }
};

export default app;
